package com.MyThread;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LuaGetListThread extends Thread {

    private JedisPool jedisPool;

    private int start = 0;

    private int end = 0;

    private String key;

    public LuaGetListThread(JedisPool jedisPool, int start, int end, String key) {
        this.jedisPool = jedisPool;
        this.start = start;
        this.end = end;
        this.key = key;
    }

    @Override
    public void run() {
        Jedis jedis = jedisPool.getResource();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");

        // 清空redis
        jedis.flushDB();

        int num = end - start;
        Date startDate = new Date();
        System.out.println("LuaGetListThread---->" + (num) + ";开始时间：" + sdf.format(startDate));

        String script = "local redisKes = KEYS[1];" +
                "local start = ARGV[1];" +
                "local endd = ARGV[2];" +
                "local rows = redis.call('lrange',redisKes,start,endd);" +
                "return rows";

        List<String> keyList = new ArrayList<String>();
        keyList.add(key);

        List<String> argList = new ArrayList<String>();
        argList.add(start + "");
        argList.add(end + "");

        jedis.eval(script,keyList,argList);

        Date endDate = new Date();
        System.out.println("LuaGetListThread---->" + (num) + ";结束时间：" + sdf.format(endDate));

        System.out.println("数据结构:String\n操作:Set\n" + "数据量:" + (num) + "\nkey长度:" + key.length()
                + "\nvalue长度:" + "40" + "\n耗时:" + (endDate.getTime() - startDate.getTime()) + "/ms\n");

        jedis.close();
    }
}
